package com.bw.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.bw.dao.ProductReplyMapper;
import com.bw.dao.StoreReplyMapper;
import com.bw.pojo.comment.ProductReply;
import com.bw.pojo.comment.StoreReply;
import com.bw.service.comment.ProductReplyService;
import com.bw.service.comment.StoreReplyService;
import com.bw.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class StoreReplyServiceImpl implements StoreReplyService {
    @Autowired
    private StoreReplyMapper storeReplyMapper;
    @Autowired
    private ProductReplyMapper productReplyMapper;
    @Reference
    private ProductReplyService productReplyService;
    @Autowired
    private IdWorker idWorker;

    @Override
    public StoreReply findById(String Id) {//根据id查询商家评论
        return storeReplyMapper.selectByPrimaryKey(Id);
    }
    @Override
    public List<StoreReply> findByProductReplyId(Map<String, Object> searchMap) {//根据用户评论id查询商家评论
        Example example = createExample(searchMap);
        return storeReplyMapper.selectByExample(example);
    }
    /**
     * 添加管理员评论
     */
    @Override
    public void add(StoreReply storeReply) {
        storeReply.setId(idWorker.nextId()+"");
        storeReplyMapper.insert(storeReply);
        ProductReply productReply=productReplyService.findById(storeReply.getReplyId());
        productReply.setIsReply("1");
        productReplyService.update(productReply);
    }
    /**
     * 删除管理员评论
     */
    public void delete(String id) {
        ProductReply productReply=productReplyService.findById(storeReplyMapper.selectByPrimaryKey(id).getReplyId());
        productReply.setIsReply("0");
        productReplyService.update(productReply);
        storeReplyMapper.deleteByPrimaryKey(id);
    }
    /**
     * 还原管理员评论
     */
    public void restore(String id) {
        ProductReply productReply=productReplyService.findById(storeReplyMapper.selectByPrimaryKey(id).getReplyId());
        productReply.setIsReply("1");
        productReplyService.update(productReply);
    }





    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(StoreReply.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 用户评论id
            if(searchMap.get("replyId")!=null && !"".equals(searchMap.get("replyId"))){
                criteria.andEqualTo("replyId",searchMap.get("replyId"));
            }
//            // 品牌图片地址
//            if(searchMap.get("image")!=null && !"".equals(searchMap.get("image"))){
//                criteria.andLike("image","%"+searchMap.get("image")+"%");
//            }
//            // 品牌的首字母
//            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))){
//                criteria.andLike("letter","%"+searchMap.get("letter")+"%");
//            }
//
//            // 品牌id
//            if(searchMap.get("id")!=null ){
//                criteria.andEqualTo("id",searchMap.get("id"));
//            }
//            // 排序
//            if(searchMap.get("seq")!=null ){
//                criteria.andEqualTo("seq",searchMap.get("seq"));
//            }

        }
        return example;
    }



}
